package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult findDishByPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<Dish> dishById = dishMapper.findDishByPage(dishPageQueryDTO);

        PageResult pr = new PageResult(dishById.getTotal(),dishById.getResult());
        return pr;
    }

    @Override
    public int addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        Long currentId = BaseContext.getCurrentId();
        dish.setCreateUser(currentId);
        dish.setUpdateUser(currentId);
        int row = dishMapper.addDish(dish);
        return row;
    }

    @Override
    public DishVO findDishById(Long id) {

        DishVO dishById = dishMapper.findDishById(id);
        return dishById;
    }

    @Override
    @Transactional
    public int deleteDish(List<Long> ids) {
        int count1 = dishMapper.selectCountByIdAndStatus(ids, StatusConstant.ENABLE);
        if(count1 > 0){
            throw new RuntimeException("哒咩");
        }
        Integer count2 = setmealMapper.countByCategoryId(ids);
        if(count2 > 0){
            throw new RuntimeException("哒咩哟");
        }
        dishMapper.deleteDishFlavor(ids);
        int row = dishMapper.deleteDish(ids);
        return row;
    }

    @Override
    public int updateStatus(Integer status, Long id) {
        int row = dishMapper.updateStatus(status, id);
        return row;
    }

    @Override
    public int updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        int row = dishMapper.updateDish(dish);
        dishMapper.deleteDishFlavor(Collections.singletonList(dish.getId()));

        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishMapper.addNewFlavor(flavors);
        return row;
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        List<Dish> byCategoryId = dishMapper.findByCategoryId(categoryId);

        return byCategoryId;
    }
}
